package com.biyao.service.model.product.imp;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import java.util.HashMap;
import java.util.Map;

import com.biyao.schedulingdao.product.TProductionPlanIMapper;
import com.biyao.schedulingentity.base.TGrinderEntity;
import com.biyao.schedulingentity.base.TRollerRacksEntity;
import com.biyao.schedulingentity.product.ProductionPlanIT;
import com.biyao.schedulingentity.product.TProductionPlanIEntity;
import com.biyao.service.model.base.TGrinderMessage;
import com.biyao.service.model.base.TRollerRacksMessage;
import com.biyao.service.model.product.ProductionPlanIMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.ArrayList;

/**
 * @author code generator
 * @date 2019-09-18 14:27:12
 */
@Service
public class TProductionPlanIService extends ServiceImpl<TProductionPlanIMapper, TProductionPlanIEntity> implements ProductionPlanIMessage {

    @Autowired
    private TRollerRacksMessage tRollerRacksMessage;
    @Autowired
    private TGrinderMessage tGrinderMessage;

    public Page list(int pageNum, int pageSize) {

        EntityWrapper<TProductionPlanIEntity> tProductionPlanIentityWrapper = new EntityWrapper<>();
        Page page = selectPage(new Page(pageNum, pageSize), tProductionPlanIentityWrapper);
        page.setRecords(formatRecords(page.getRecords()));
        return page;
    }

    List formatRecords(List<TProductionPlanIEntity> list) {
        List resList = new ArrayList();
        for (TProductionPlanIEntity tProductionPlanIEntity : list) {
            Map map = formatEntity(tProductionPlanIEntity);
            resList.add(map);
        }
        return resList;
    }

    Map formatEntity(TProductionPlanIEntity tProductionPlanIEntity) {
        Map map = new HashMap(16);
        //
        if (tProductionPlanIEntity.getId() != null) {
            map.put("id", tProductionPlanIEntity.getId());
        }
        //t_production_plan.id
        if (tProductionPlanIEntity.getProductionPlanId() != null) {
            map.put("productionPlanId", tProductionPlanIEntity.getProductionPlanId());
        }
        //序号
        if (tProductionPlanIEntity.getSequence() != null) {
            map.put("sequence", tProductionPlanIEntity.getSequence());
        }
        //起始时间
        if (tProductionPlanIEntity.getStartTime() != null) {
            map.put("startTime", tProductionPlanIEntity.getStartTime());
        }
        //结束时间
        if (tProductionPlanIEntity.getEndTime() != null) {
            map.put("endTime", tProductionPlanIEntity.getEndTime());
        }
        //任务代号(001:移动轧辊,002磨辊)
        if (tProductionPlanIEntity.getTaskCode() != null) {
            map.put("taskCode", tProductionPlanIEntity.getTaskCode());
        }
        if (tProductionPlanIEntity.getParam0() != null) {
            map.put("param0", tProductionPlanIEntity.getParam0());
        }
        if (tProductionPlanIEntity.getParam1() != null) {
            map.put("param1", tProductionPlanIEntity.getParam1());
        }
        //
        if (tProductionPlanIEntity.getParam1() != null) {
            map.put("param1", tProductionPlanIEntity.getParam1());
        }
        //
        if (tProductionPlanIEntity.getParam2() != null) {
            map.put("param2", tProductionPlanIEntity.getParam2());
        }
        //
        if (tProductionPlanIEntity.getParam3() != null) {
            map.put("param3", tProductionPlanIEntity.getParam3());
        }
        //
        if (tProductionPlanIEntity.getParam4() != null) {
            map.put("param4", tProductionPlanIEntity.getParam4());
        }
        //
        if (tProductionPlanIEntity.getParam5() != null) {
            map.put("param5", tProductionPlanIEntity.getParam5());
        }
        if (tProductionPlanIEntity.getParam6() != null) {
            map.put("param6", tProductionPlanIEntity.getParam6());
        }
        if (tProductionPlanIEntity.getParam7() != null) {
            map.put("param7", tProductionPlanIEntity.getParam7());
        }
        //备注
        if (tProductionPlanIEntity.getRemark() != null) {
            map.put("remark", tProductionPlanIEntity.getRemark());
        }
        //
        if (tProductionPlanIEntity.getCreateTime() != null) {
            map.put("createTime", tProductionPlanIEntity.getCreateTime());
        }
        //
        if (tProductionPlanIEntity.getUpdateTime() != null) {
            map.put("updateTime", tProductionPlanIEntity.getUpdateTime());
        }
        //
        if (tProductionPlanIEntity.getCreateBy() != null) {
            map.put("createBy", tProductionPlanIEntity.getCreateBy());
        }
        //
        if (tProductionPlanIEntity.getUpdateBy() != null) {
            map.put("updateBy", tProductionPlanIEntity.getUpdateBy());
        }
        return map;
    }

    @Override
    public List<Map> selectProductionI(int pageNum, int pageSize) {
        EntityWrapper<TProductionPlanIEntity> tProductionPlanIentityWrapper = new EntityWrapper<>();
        Page page = selectPage(new Page(pageNum, pageSize), tProductionPlanIentityWrapper);
        return formatRecords(page.getRecords());
    }

    @Override
    public void addProductionI(TProductionPlanIEntity tProductionPlanEntity) {
        String param2 = tProductionPlanEntity.getParam2();
        String param3 = tProductionPlanEntity.getParam3();

        if (!StringUtils.isEmpty(param2) && param2.contains("+")) {
            String param7 = param2.replace("+", "");
            tProductionPlanEntity.setParam7(param7);
            tProductionPlanEntity.setParam2(null);
        }
        if (!StringUtils.isEmpty(param3) && param3.contains("+")) {
            String param8 = param3.replace("+", "");
            tProductionPlanEntity.setParam8(param8);
            tProductionPlanEntity.setParam3(null);

        }
        insert(tProductionPlanEntity);
    }

    @Override
    public void updateProductionI(TProductionPlanIEntity tProductionPlanEntity) {
        updateById(tProductionPlanEntity);
    }


    @Override
    public List<TProductionPlanIEntity> selectProductionByPlanId(Integer id) {
        EntityWrapper<TProductionPlanIEntity> tProductionPlanIentityWrapper = new EntityWrapper<>();
        tProductionPlanIentityWrapper.eq("production_plan_id", id).orderBy("sequence");
        List<TProductionPlanIEntity> tProductionPlanIEntityList = selectList(tProductionPlanIentityWrapper);
        return tProductionPlanIEntityList;
    }

    @Override
    public TProductionPlanIEntity selectProductionIId(Integer id) {
        return selectById(id);
    }

    @Override
    public void deleteByProductId(Integer id) {
        EntityWrapper<TProductionPlanIEntity> tProductionPlanIentityWrapper = new EntityWrapper<>();
        tProductionPlanIentityWrapper.eq("production_plan_id", id);
        delete(tProductionPlanIentityWrapper);
    }

    @Override
    public Map selectProductionDetailAndJudgeIsImplementByProductId(Integer id) {
//        EntityWrapper<TProductionPlanIEntity> tProductionPlanIentityWrapper=new EntityWrapper<>();
//        tProductionPlanIentityWrapper.eq("production_plan_id",id).eq("execution_position",true);
//         tProductionPlanIentityWrapper.orderBy("sequence",true);
//        List<TProductionPlanIEntity> list=selectList(tProductionPlanIentityWrapper);
//        if(list.size()>0){
//            Map map=judgeIsImplement(list.get(0));
//            if(!(Boolean) map.get("flag")){
//                return map;
//            }
//            map.put("entity",list.get(0));
//            return map;
//        }else {
//            //设置模版从当前重新开始标识
//           Integer sequence= baseMapper.selectProductionFirstSequence(id);
//           baseMapper.updateProductionINextState(true,id,sequence);
//        }
//        Map map=new HashMap();
//        map.put("flag",false);

        Boolean flag = true;
        EntityWrapper<TProductionPlanIEntity> tProductionPlanIentityWrapper = new EntityWrapper<>();
        tProductionPlanIentityWrapper.eq("production_plan_id", id);
        tProductionPlanIentityWrapper.orderBy("sequence", true);
        List<TProductionPlanIEntity> list = selectList(tProductionPlanIentityWrapper);
        //天车T1任务
        List<TProductionPlanIEntity> t1List = new ArrayList<>();
        for (TProductionPlanIEntity tProductionPlanIEntity : list) {
            if ("1000".equals(tProductionPlanIEntity.getParam1()) || "100".equals(tProductionPlanIEntity.getParam1()) || "101".equals(tProductionPlanIEntity.getParam1())) {
                t1List.add(tProductionPlanIEntity);
            }
        }
        for (TProductionPlanIEntity tProductionPlanIEntity : t1List) {
            if (tProductionPlanIEntity.getExecutionPosition()&&StringUtils.isEmpty(tProductionPlanIEntity.getParam0())) {
                Map map = judgeIsImplement(tProductionPlanIEntity);
                if (!(Boolean) map.get("flag")) {
                    return map;
                }
                flag = false;
                map.put("entity", tProductionPlanIEntity);
                if(tProductionPlanIEntity.getId()==t1List.get(t1List.size()-1).getId()){
                    tProductionPlanIEntity.setParam0("1");
                    updateById(tProductionPlanIEntity);
                }
                return map;
            }
        }
        if (flag) {
            //设置模版从当前重新开始标识

            for(TProductionPlanIEntity tProductionPlanIEntity : t1List){
                if(!StringUtils.isEmpty(tProductionPlanIEntity.getParam0())){
                    tProductionPlanIEntity.setParam0("");
                    updateById(tProductionPlanIEntity);
                }
                if (tProductionPlanIEntity.getExecutionPosition()) {
                    updateNowProductionPlanIPostion(tProductionPlanIEntity);
                }
            }
//            Integer sequence = baseMapper.selectProductionFirstSequence(id);
            if(t1List.size()>0)
            baseMapper.updateProductionINextState(true, id, t1List.get(0).getSequence());
        }
        Map map = new HashMap();
        map.put("flag", false);
        return map;

    }

    @Override
    public Map selectProductionDetailAndJudgeIsImplementByProductIdT2(Integer id) {
        Boolean flag = true;
        EntityWrapper<TProductionPlanIEntity> tProductionPlanIentityWrapper = new EntityWrapper<>();
        tProductionPlanIentityWrapper.eq("production_plan_id", id);
        tProductionPlanIentityWrapper.orderBy("sequence", true);
        List<TProductionPlanIEntity> list = selectList(tProductionPlanIentityWrapper);
        List<TProductionPlanIEntity> t2List = new ArrayList<>();

        for (TProductionPlanIEntity tProductionPlanIEntity : list) {
            if ("1001".equals(tProductionPlanIEntity.getParam1()) || "102".equals(tProductionPlanIEntity.getParam1()) || "103".equals(tProductionPlanIEntity.getParam1())) {
                t2List.add(tProductionPlanIEntity);
            }
        }
        for (TProductionPlanIEntity tProductionPlanIEntity : t2List) {
            if (tProductionPlanIEntity.getExecutionPosition()&&StringUtils.isEmpty(tProductionPlanIEntity.getParam0())) {
                Map map = judgeIsImplement(tProductionPlanIEntity);
                if (!(Boolean) map.get("flag")) {
                    return map;
                }
                flag = false;
                map.put("entity", tProductionPlanIEntity);
                if(tProductionPlanIEntity.getId()==t2List.get(t2List.size()-1).getId()){
                    tProductionPlanIEntity.setParam0("1");
                    updateById(tProductionPlanIEntity);
                }
                return map;
            }
        }
        if (flag) {
            //设置模版从当前重新开始标识
            for (TProductionPlanIEntity tProductionPlanIEntity : t2List) {
                if (tProductionPlanIEntity.getExecutionPosition()) {
                    updateNowProductionPlanIPostion(tProductionPlanIEntity);
                }
                if(!StringUtils.isEmpty(tProductionPlanIEntity.getParam0())){
                    tProductionPlanIEntity.setParam0("");
                    updateById(tProductionPlanIEntity);
                }
            }
            if(t2List.size()>0)
            baseMapper.updateProductionINextState(true, id, t2List.get(0).getSequence());

        }
        Map map = new HashMap();
        map.put("flag", false);
        return map;
    }

    @Override
    public List<ProductionPlanIT> selectProductTranslation(Integer id) {
        EntityWrapper<TProductionPlanIEntity> tProductionPlanIentityWrapper = new EntityWrapper<>();
        tProductionPlanIentityWrapper.eq("production_plan_id", id);
        List<TProductionPlanIEntity> allList = selectList(tProductionPlanIentityWrapper);
        List<TProductionPlanIEntity> list=new ArrayList<>();
        List<TProductionPlanIEntity> endList=new ArrayList<>();
         Integer idFlag=0;
        for(TProductionPlanIEntity tProductionPlanIEntity:allList){
            if ("1000".equals(tProductionPlanIEntity.getParam1()) || "100".equals(tProductionPlanIEntity.getParam1()) || "101".equals(tProductionPlanIEntity.getParam1())) {
                list.add(tProductionPlanIEntity);
                if(tProductionPlanIEntity.getExecutionPosition()){
                    idFlag=tProductionPlanIEntity.getId();
                }
            }

        }
        for(TProductionPlanIEntity tProductionPlanIEntity:list){
           if(tProductionPlanIEntity.getId()>=idFlag){
               endList.add(tProductionPlanIEntity);
           }
        }
        return translation(endList);
    }

    @Override
    public List<ProductionPlanIT> selectProductionTranslationT2(Integer id) {
        EntityWrapper<TProductionPlanIEntity> tProductionPlanIentityWrapper = new EntityWrapper<>();
        tProductionPlanIentityWrapper.eq("production_plan_id", id);
        List<TProductionPlanIEntity> allList = selectList(tProductionPlanIentityWrapper);
        List<TProductionPlanIEntity> list=new ArrayList<>();
        List<TProductionPlanIEntity> endList=new ArrayList<>();
        Integer idFlag=0;
        for(TProductionPlanIEntity tProductionPlanIEntity:allList){
            if ("1001".equals(tProductionPlanIEntity.getParam1()) || "102".equals(tProductionPlanIEntity.getParam1()) || "103".equals(tProductionPlanIEntity.getParam1())) {
                list.add(tProductionPlanIEntity);
                if(tProductionPlanIEntity.getExecutionPosition()){
                    idFlag=tProductionPlanIEntity.getId();
                }
            }
        }
        for(TProductionPlanIEntity tProductionPlanIEntity:list){
            if(tProductionPlanIEntity.getId()>=idFlag){
                endList.add(tProductionPlanIEntity);
            }
        }
        return translation(endList);
    }

    private synchronized List<ProductionPlanIT> translation(List<TProductionPlanIEntity> list){
        List<ProductionPlanIT> backList = new ArrayList<>();
        for (TProductionPlanIEntity planIEntity : list) {
            ProductionPlanIT productionPlanIT = new ProductionPlanIT();
            if ("001".equals(planIEntity.getTaskCode())) {
                productionPlanIT.setTaskName("移动轧辊");
            } else if ("002".equals(planIEntity.getTaskCode())) {
                productionPlanIT.setTaskName("磨辊");
            }
            if ("1".equals(planIEntity.getParam4())) {
                productionPlanIT.setStartUpOrDown("上");
            } else if ("2".equals(planIEntity.getParam4())) {
                productionPlanIT.setStartUpOrDown("下");
            } else if ("3".equals(planIEntity.getParam4())) {
                productionPlanIT.setStartUpOrDown("一对");
            } else {
                productionPlanIT.setStartUpOrDown("");
            }
            if ("1".equals(planIEntity.getParam5())) {
                productionPlanIT.setEndUpOrDown("上");
            } else if ("2".equals(planIEntity.getParam5())) {
                productionPlanIT.setEndUpOrDown("下");
            } else if ("3".equals(planIEntity.getParam5())) {
                productionPlanIT.setEndUpOrDown("一对");
            } else {
                productionPlanIT.setEndUpOrDown("");
            }
            //辊架和辊架移动
            if (!StringUtils.isEmpty(planIEntity.getParam2()) && !StringUtils.isEmpty(planIEntity.getParam3())) {
                TRollerRacksEntity tRollerRacksEntity = tRollerRacksMessage.selectTRollerEntityById(Integer.parseInt(planIEntity.getParam2()));
                productionPlanIT.setStartPosition(tRollerRacksEntity.getCode());
                TRollerRacksEntity tRollerRacksEntity2 = tRollerRacksMessage.selectTRollerEntityById(Integer.parseInt(planIEntity.getParam3()));
                productionPlanIT.setEndPosition(tRollerRacksEntity2.getCode());
            }
            //辊架到磨床
            if (!StringUtils.isEmpty(planIEntity.getParam2()) && StringUtils.isEmpty(planIEntity.getParam3()) && !StringUtils.isEmpty(planIEntity.getParam8())) {
                TRollerRacksEntity tRollerRacksEntity = tRollerRacksMessage.selectTRollerEntityById(Integer.parseInt(planIEntity.getParam2()));
                productionPlanIT.setStartPosition(tRollerRacksEntity.getCode());
                TGrinderEntity tGrinderEntity = tGrinderMessage.selectEntryById(Integer.parseInt(planIEntity.getParam8()));
                productionPlanIT.setEndPosition(tGrinderEntity.getCode());
            }
            //磨床到辊架
            if (!StringUtils.isEmpty(planIEntity.getParam3()) && !StringUtils.isEmpty(planIEntity.getParam7()) && StringUtils.isEmpty(planIEntity.getParam2())) {
                TGrinderEntity tGrinderEntity = tGrinderMessage.selectEntryById(Integer.parseInt(planIEntity.getParam7()));
                productionPlanIT.setStartPosition(tGrinderEntity.getCode());
                TRollerRacksEntity tRollerRacksEntity = tRollerRacksMessage.selectTRollerEntityById(Integer.parseInt(planIEntity.getParam3()));
                productionPlanIT.setEndPosition(tRollerRacksEntity.getCode());
            }
            if (!StringUtils.isEmpty(planIEntity.getParam1())) {
                //天车
                if (Integer.parseInt(planIEntity.getParam1()) == 1000) {
                    productionPlanIT.setDeviceName("T1");
                } else if (Integer.parseInt(planIEntity.getParam1()) == 1001) {
                    productionPlanIT.setDeviceName("T2");
                } else if (Integer.parseInt(planIEntity.getParam1()) < 1000) {
                    TGrinderEntity tGrinderEntity = tGrinderMessage.selectEntryById(Integer.parseInt(planIEntity.getParam1()));
                    productionPlanIT.setDeviceName(tGrinderEntity.getCode());
                }
            }
            if (productionPlanIT.getEndPosition() == null) {
                productionPlanIT.setEndPosition("");
            }
            if (productionPlanIT.getEndUpOrDown() == null) {
                productionPlanIT.setEndUpOrDown("");
            }
            if (productionPlanIT.getStartPosition() == null) {
                productionPlanIT.setStartPosition("");
            }
            if (productionPlanIT.getStartUpOrDown() == null) {
                productionPlanIT.setStartUpOrDown("");
            }
            backList.add(productionPlanIT);

        }
        return backList;
    }


    //判断自动模式是否可以执行
    public Map judgeIsImplement(TProductionPlanIEntity tProductionPlanIEntity) {
        Map map = new HashMap();
        map.put("flag", true);
        return map;
    }

    public void updateNowProductionPlanIPostion(TProductionPlanIEntity tProductionPlanIEntity) {
        //更新当前状态
        tProductionPlanIEntity.setExecutionPosition(false);
        updateById(tProductionPlanIEntity);
    }

    public void updateNextProductionPlanIPostion(TProductionPlanIEntity tProductionPlanIEntity) {

        Integer sequence = selectLatelySequence(tProductionPlanIEntity.getId(),tProductionPlanIEntity.getProductionPlanId());

        baseMapper.updateProductionINextState(true, tProductionPlanIEntity.getProductionPlanId(), sequence);

    }

    @Override
    @Transactional
    public void resetProductionPlan(Integer id) {
         //更新生产计划表执行位置为空
        baseMapper.resetProductionI(false,id);
        //查询T1
        Integer t1Id=baseMapper.selectFirstSequenceId(id,"1000");
        if(t1Id!=null){
            TProductionPlanIEntity t1Entity=baseMapper.selectById(t1Id);
            t1Entity.setExecutionPosition(true);
            baseMapper.updateById(t1Entity);
        }
        //查询T2
        Integer t2Id=baseMapper.selectFirstSequenceId(id,"1001");
        if(t2Id!=null){
            TProductionPlanIEntity t1Entity=baseMapper.selectById(t2Id);
            t1Entity.setExecutionPosition(true);
            baseMapper.updateById(t1Entity);
        }

    }

    public Integer selectLatelySequence(Integer id,Integer productionPlanId) {
        TProductionPlanIEntity tProductionPlanIEntity=selectById(id);
        Integer sequence=tProductionPlanIEntity.getSequence();
        EntityWrapper<TProductionPlanIEntity> tProductionPlanIentityWrapper = new EntityWrapper<>();
        tProductionPlanIentityWrapper.eq("production_plan_id", productionPlanId);
        tProductionPlanIentityWrapper.orderBy("sequence", true);
        List<TProductionPlanIEntity> allList = selectList(tProductionPlanIentityWrapper);
        if ("1000".equals(tProductionPlanIEntity.getParam1()) || "100".equals(tProductionPlanIEntity.getParam1()) || "101".equals(tProductionPlanIEntity.getParam1())) {
         //更新T1
          for(TProductionPlanIEntity tProductionPlanIEntity1:allList){
              if (("1000".equals(tProductionPlanIEntity1.getParam1())&&tProductionPlanIEntity1.getSequence()>sequence )|| ("100".equals(tProductionPlanIEntity1.getParam1()) &&tProductionPlanIEntity1.getSequence()>sequence)|| ("101".equals(tProductionPlanIEntity1.getParam1())&&tProductionPlanIEntity1.getSequence()>sequence)) {
                  return tProductionPlanIEntity1.getSequence();
              }

          }
        }
        if ("1001".equals(tProductionPlanIEntity.getParam1()) || "102".equals(tProductionPlanIEntity.getParam1()) || "103".equals(tProductionPlanIEntity.getParam1())) {
        //更新T2
            for(TProductionPlanIEntity tProductionPlanIEntity1:allList){
                if (("1001".equals(tProductionPlanIEntity1.getParam1())&&tProductionPlanIEntity1.getSequence()>sequence )|| ("102".equals(tProductionPlanIEntity1.getParam1()) &&tProductionPlanIEntity1.getSequence()>sequence)|| ("103".equals(tProductionPlanIEntity1.getParam1())&&tProductionPlanIEntity1.getSequence()>sequence)) {
                    return tProductionPlanIEntity1.getSequence();
                }

            }
        }
        return null;
    }


}

